package com.sctvcloud.bazhong.ui.utils;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.AbsRunnable;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.ruihang.ijkplaylib.widget.IGPlayerBottomControll;
import com.ruihang.ijkplaylib.widget.IjkVideoView;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.beans.LiveInfo;

import java.lang.ref.WeakReference;
import java.util.Arrays;

/**
 * Created by liuyu on 2017/8/3.
 */

public abstract class AbsLivingBottomController implements IGPlayerBottomControll, View.OnClickListener {

    protected WeakReference<GPlayerView> playerViewWeak;
    protected WeakReference<AppCompatActivity> activityWeak;

    protected View base;

    protected ImageView playBtn;
    protected CustomFontTextView barrageBtn, fullBtn;

    protected boolean isPlaying;

    protected boolean isPortrait;
    protected boolean forbidenFull;
    /**
     * 是否已经添加到视图
     */
    protected boolean hasAdding;

    protected int barrageStateTemp = IBarrageControll.STATE_CLOSED;

    private IBarrageControll barrageControll;

    public AbsLivingBottomController(AppCompatActivity activity) {
        activityWeak = new WeakReference<AppCompatActivity>(activity);
    }

    @Override
    public void onProgressSlide(long delta) {
    }

    @Override
    public void onPlayerHide(GPlayerView playerView) {
    }

    @Override
    public boolean canChangeScreen() {
        return true;
    }

    @Override
    public void onUpdateUI(boolean isPlaying, boolean isLive, boolean isPortrait) {
        if (!hasAdding) {
            return;
        }
        this.isPlaying = isPlaying;
        this.isPortrait = isPortrait;
        if (isPlaying) {
            if (isLive) {
                playBtn.setImageResource(R.mipmap.icon_living_cr_pause_white);
            } else {
                playBtn.setImageResource(R.mipmap.icon_living_cr_pause_white);
            }
        } else {
            playBtn.setImageResource(R.mipmap.icon_living_cr_play_white);
        }
    }

    @Override
    public long syncProgress(IjkVideoView videoView) {
        return 0;
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        JLog.e("on adding this=" + this);
        playerViewWeak = new WeakReference<GPlayerView>(playerView);
        base = onCreateView(playerView, inflater, group);
//        playBtn = (ImageView) base.findViewById(R.id.living_top_img_play);
        if (barrageBtn != null) {
            barrageBtn.setOnClickListener(this);
            if (barrageStateTemp == IBarrageControll.STATE_OPENED) {
                barrageBtn.setText(R.string.close_barrage);
            }
        }
        if (fullBtn != null) {
            fullBtn.setOnClickListener(this);
        }
        if (playBtn != null) {
            playBtn.setOnClickListener(this);
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) base.getLayoutParams();
        JLog.e("base lp=" + lp + "  w=" + lp.width + "   h=" + lp.height + "  r=" + Arrays.toString(lp.getRules()));
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        group.addView(base, lp);
        hasAdding = true;
        fullBtn.setVisibility(forbidenFull ? View.GONE : View.VISIBLE);
    }

    @Override
    public boolean onShowPanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        this.isPortrait = isPortrait;
        if (base != null) {
            base.setVisibility(View.VISIBLE);
            onUpdateUI(isPlaying, isLive, isPortrait);
        }
        return false;
    }

    @Override
    public void onHidePanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        this.isPortrait = isPortrait;
        if (base != null) {
            base.setVisibility(View.GONE);
            onUpdateUI(isPlaying, isLive, isPortrait);
        }
    }

    @Override
    public void onPortrait() {
    }

    @Override
    public void onLandscape() {
    }

    @Override
    public void onDetach(ViewGroup group) {
        try {
            group.removeView(base);
        } catch (Exception e) {
            e.printStackTrace();
        }
        onDestory();
    }

    @CallSuper
    @Override
    public void onDestory() {
        if (barrageBtn != null) {
            barrageBtn.setOnClickListener(null);
        }
        if (fullBtn != null) {
            fullBtn.setOnClickListener(null);
        }
        if (playBtn != null) {
            playBtn.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == fullBtn) {
            v.setEnabled(false);
            AppCompatActivity activity = getActivityTemp();
            if (activity != null) {
                if (isPortrait) {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                } else {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                }
                v.postDelayed(new AbsRunnable(v) {
                    @Override
                    public void run() {
                        try {
                            ((View) obj).setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, 800);
            }
        } else if (v == barrageBtn) {
            if (barrageControll != null) {
                if (barrageStateTemp == IBarrageControll.STATE_CLOSED) {
                    barrageControll.openOrCloseBarrage(true);
                } else if (barrageStateTemp == IBarrageControll.STATE_OPENED) {
                    barrageControll.openOrCloseBarrage(false);
                }
            }
        } else if (v == playBtn) {
            GPlayerView playerView = getPlayerTemp();
            JLog.e("playerView=" + playerView + "  isplaying=" + isPlaying);
            if (playerView != null) {
                if (isPlaying) {
                    playerView.pausePlay();
                } else {
                    playerView.startPlay();
                }
            }
        }
    }

    @NonNull
    protected abstract View onCreateView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group);

    public void initInfo(LiveInfo info) {
    }

    public void setForbidenFull(boolean forbidenFull) {
        boolean b = this.forbidenFull != forbidenFull;
        this.forbidenFull = forbidenFull;
        if (b && fullBtn != null) {
            fullBtn.setVisibility(forbidenFull ? View.GONE : View.VISIBLE);
        }
    }

    protected GPlayerView getPlayerTemp() {
        if (playerViewWeak != null) {
            return playerViewWeak.get();
        }
        return null;
    }

    protected AppCompatActivity getActivityTemp() {
        if (activityWeak != null) {
            return activityWeak.get();
        }
        return null;
    }

    public void setBarrageState(int state) {
        JLog.e("state=" + state + " temp=" + barrageStateTemp + "\n" + barrageBtn);
        if (barrageStateTemp != state) {
            if (state == IBarrageControll.STATE_CLOSED) {
                barrageStateTemp = state;
                if (barrageBtn != null) {
                    barrageBtn.setText(R.string.show_barrage);
                }
            } else if (state == IBarrageControll.STATE_OPENED) {
                barrageStateTemp = state;
                if (barrageBtn != null) {
                    barrageBtn.setText(R.string.close_barrage);
                }
            }
        }
    }

    public AbsLivingBottomController setBarrageControll(IBarrageControll barrageControll) {
        this.barrageControll = barrageControll;
        return this;
    }
}
